package SecondeStage.Networkprogramming.Socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Tcp01Client {
    public static void main(String[] args) throws IOException {
        // 1. 连接服务端(ip,端口)
        // 连接本机的 9999端口，如果连接成功，返回Socket对象
        Socket socket=new Socket(InetAddress.getLocalHost(),9999);
        System.out.println("socket返回="+socket.getClass());
        // 2. 连接上后，生产Socket，通过socket.getOutputStream()得到和socket对象关联的输出流对象
        OutputStream outputStream=socket.getOutputStream();
        // 3. 通过输出流，写入数据到 数据通道
        outputStream.write("hello,server".getBytes());
        // 设置结束标记
        socket.shutdownOutput();
        // 4. 获取和socket相关联的输入流，读取数据(字节)并显示
        InputStream inputStream=socket.getInputStream();
        byte[] buf = new byte[1024];
        int reanlen =0;
        while ((reanlen = inputStream.read(buf))!= -1){
            System.out.println(new String(buf,0,reanlen));
        }
        // 5.. 关闭流对象和socket
        outputStream.close();
        inputStream.close();
        socket.close();
        System.out.println("客户端退出.......");
    }
}
